﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;

namespace MineSweeping.AppCode.Tops
{
    public class LocalTop : GameTop
    {
        private const int topCount = 20;

        private const string topsKey = "topsKey";

        private static readonly IDictionary<Difficulty, List<TopItem>> tops;

        static LocalTop()
        {
            // if not exist, create an empty tops
            if(!IsolatedStorageSettings.ApplicationSettings.Contains(topsKey))
            {
                tops = new Dictionary<Difficulty, List<TopItem>>();
                tops[Difficulty.Easy] = new List<TopItem>();
                tops[Difficulty.Medium] = new List<TopItem>();
                tops[Difficulty.Hard] = new List<TopItem>();

                IsolatedStorageSettings.ApplicationSettings[topsKey] = tops;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }

            // get local tops from IsolatedStorageSettings
            tops = IsolatedStorageSettings.ApplicationSettings[topsKey] as IDictionary<Difficulty, List<TopItem>>;
        }

        private static List<TopItem> GetTopList(Difficulty difficulty)
        {
            if(tops[difficulty] == null)
            {
                return new List<TopItem>();
            }

            return tops[difficulty];
        }

        public override List<TopItem> GetTopItemsByLevel(Difficulty difficult)
        {
            List<TopItem> topList = new List<TopItem>(topCount);

            foreach (var topItem in GetTopList(difficult))
            {
                topList.Add(topItem);
            }

            topList.Sort(new TopItemSort());

            // 补全Tops的项
            if(topList.Count < topCount)
            {
                for (int i = topList.Count; i < topCount; i++)
                {
                    var topItem = new TopItem
                                      {
                                          Score = -1,
                                          CreateDate = DateTime.MaxValue
                                      };

                    topList.Add(topItem);
                }
            }

            foreach (var topItem in topList)
            {
                topItem.Id = topList.IndexOf(topItem) + 1;
            }

            return topList;
        }

        public override void AddToTop(TopItem topItem)
        {
            Difficulty difficult = topItem.Difficulty;

            List<TopItem> currentTop = GetTopList(difficult);

            currentTop.Add(topItem);

            currentTop.Sort(new TopItemSort());

            currentTop.TrimExcess();
            tops[difficult] = currentTop;

            IsolatedStorageSettings.ApplicationSettings[topsKey] = tops;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }

    public class TopItemSort : IComparer<TopItem>
    {
        public int Compare(TopItem x, TopItem y)
        {
            if (x.Score > y.Score) return 1;
            if ((x.Score == y.Score) && (x.CreateDate > y.CreateDate)) return 1;
            if ((x.Score == y.Score) && (x.CreateDate < y.CreateDate)) return -1;
            if (x.Score < y.Score) return -1;

            return 0;
        }
    }  
}
